//
//  GHChongZhiRecordController.m
//  GodHorses
//
//  Created by Mac on 2017/11/16.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHChongZhiRecordController.h"
#import "GHChongZhiRecordCell.h"
#import "GHRechargeRecordModel.h"
@interface GHChongZhiRecordController ()
@property(nonatomic, strong) NSMutableArray <GHRechargeRecordModel*>                    *dataSouce;
@property(nonatomic, strong) GHNoResultView                     *noResultView;
@end

@implementation GHChongZhiRecordController


#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"充值记录";
    [self configSubView];
    self.dataSouce = [NSMutableArray new];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    
    self.noResultView = [[GHNoResultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 350) title:@"暂无充值记录" buttonTitle:@"快去充值吧" buttonHandle:^(NSString *titleString) {
        
    }];
    self.noResultView.hidden = YES;
    [self.tableView addSubview:self.noResultView];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendRechargeRecordRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [GHChongZhiRecordCell CellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    GHChongZhiRecordCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[GHChongZhiRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.dataSouce[indexPath.section];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Private Method
- (void)reloadData{
    self.noResultView.hidden = self.dataSouce.count;
    [self.tableView reloadData];
}
#pragma mark --NetWork Method
- (void)sendRechargeRecordRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [dic setObject:@(1) forKey:@"page"];
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/personal/recharge_record"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                  [SVProgressHUD dismiss];
                                                 NSDictionary * datadic = responseObject[@"data"];
                                                 if ([datadic.allKeys containsObject:@"recharge_data"]) {
                                                     self.dataSouce = [NSMutableArray new];
                                                     NSArray * array = datadic[@"recharge_data"];
                                                     for (NSDictionary * modelDic in array) {
                                                         GHRechargeRecordModel * model = [GHRechargeRecordModel mj_objectWithKeyValues:modelDic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                     [self reloadData];
                                                 }

                                                 [self.tableView.mj_header endRefreshing];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [self reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}

@end
